package com.yesep.utils;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.CharacterIterator;
import java.text.DecimalFormat;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: envm
 * @description: 字符串工具类
 * @author: quliang
 * @create: 2019-07-17 15:40
 **/
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    private static int RESERVESNUM = 12;
    private static char[] reserve;
    private static final char[] QUOTE_ENCODE = "&quot;".toCharArray();
    private static final char[] AMP_ENCODE = "&amp;".toCharArray();
    private static final char[] LT_ENCODE = "&lt;".toCharArray();
    private static final char[] GT_ENCODE = "&gt;".toCharArray();
    public static final long SECOND = 1000L;
    public static final long MINUTE = 60000L;
    public static final long HOUR = 3600000L;
    public static final long DAY = 86400000L;
    public static final long WEEK = 604800000L;
    public static final long KBYTES = 1024L;
    private static MessageDigest digest = null;
    private static final int fillchar = 61;
    private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    static {
        reserve = new char[RESERVESNUM];
        reserve[0] = '!';
        reserve[1] = '#';
        reserve[2] = '%';
        reserve[3] = '*';
        reserve[4] = '/';
        reserve[5] = ':';
        reserve[6] = '?';
        reserve[7] = '&';
        reserve[8] = ';';
        reserve[9] = '=';
        reserve[10] = '@';
        reserve[11] = ' ';
    }

    public StringUtils() {
    }

    public static String trim(String str) {
        return str == null ? "" : str.trim();
    }

    public static String webTrim(String str) {
        str = trim(str);
        return str.equals("") ? "&nbsp;" : str;
    }

    public static String webTrim(Object obj) {
        String str = trim(obj);
        return str.equals("") ? "&nbsp;" : str;
    }

    public static String trim(String str, String defult) {
        return str == null ? trim(defult) : str.trim();
    }

    public static String trim(Object str) {
        return str == null ? "" : str.toString().trim();
    }

    public static String trim_web(String str) {
        String tmp = trim(str);
        return tmp.equals("") ? "&nbsp;" : tmp;
    }

    public static String trim(Object str, String defult) {
        return str == null ? trim(defult) : str.toString().trim();
    }

    public static String nonNull(String str) {
        return str == null ? "" : str;
    }

    public static StringBuffer getStringBuffer(InputStream in) throws IOException {
        if (in == null) {
            return new StringBuffer("");
        } else {
            StringBuffer result = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            char[] buf = new char[1024];
            boolean quit = false;

            while (!quit) {
                int len = reader.read(buf);
                if (len < 0) {
                    quit = true;
                } else {
                    result.append(buf, 0, len);
                }
            }

            reader.close();
            return result;
        }
    }

    public static String getText(InputStream in) throws IOException {
        return getStringBuffer(in).toString();
    }

    public static String getText(Reader reader) throws IOException {
        if (reader == null) {
            return "";
        } else {
            StringBuffer dat = new StringBuffer();
            char[] buf = new char[1024];

            while (true) {
                int l = reader.read(buf);
                if (l < 0) {
                    return dat.toString();
                }

                dat.append(buf, 0, l);
            }
        }
    }

    public static int getAsInt(String str) {
        return getAsInt(str, -1);
    }

    public static int toHexString(String data) throws Exception {
        String hexString = null;

        String[] escapeArry = {"\b", "\t", "\n", "\f", "\r"};

        boolean flag = false;
        for (String esacape : escapeArry) {
            if (esacape.contains(data)) {
                flag = true;
                break;
            }
        }
        if (flag) throw new Exception("参数字符串不能包含转义字符");
// 16进制字符
        char[] hexArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        StringBuilder sb = new StringBuilder();
// String-->byte[]
        byte[] bs = data.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(hexArray[bit]);
            bit = bs[i] & 0x0f;
            sb.append(hexArray[bit]);
        }
        hexString = sb.toString();
        return Integer.valueOf(hexString);
    }


    public static int getAsInt(String str, int defaultv) {
        if (str != null && !"".equals(str)) {
            try {
                return Integer.parseInt(str, 10);
            } catch (Exception var3) {
                return defaultv;
            }
        } else {
            return defaultv;
        }
    }

    public static long getAsLong(String str) {
        return getAsLong(str, -1L);
    }

    public static long getAsLong(String str, long defaultv) {
        if (str != null && !"".equals(str)) {
            try {
                return Long.parseLong(str, 10);
            } catch (Exception var4) {
                return defaultv;
            }
        } else {
            return defaultv;
        }
    }

    public static float getAsFloat(String str) {
        return getAsFloat(str, 0.0F);
    }

    public static float getAsFloat(String str, Float defaultv) {
        if (str != null && !"".equals(str)) {
            try {
                return Float.parseFloat(str);
            } catch (Exception var3) {
                return defaultv;
            }
        } else {
            return defaultv;
        }
    }

    public static double getAsDouble(String str) {
        return getAsDouble(str, 0.0D);
    }

    public static double getAsDouble(String str, double defaultv) {
        if (str != null && !"".equals(str)) {
            try {
                return Double.parseDouble(str);
            } catch (Exception var4) {
                return defaultv;
            }
        } else {
            return defaultv;
        }
    }

    public static String getFormatNumber(Number num, String format) {
        if (num == null) {
            return "";
        } else if (format == null) {
            return String.valueOf(num);
        } else {
            DecimalFormat df = new DecimalFormat(format);
            return df.format(num);
        }
    }

    public static String[] split(String str, String seperator) {
        return split(str, seperator, false);
    }

    public static String[] split(String str, String seperator, boolean tail) {
        if (str != null && !str.equals("") && !trim(seperator).equals("")) {
            if (!tail && str.endsWith(seperator)) {
                str = str.substring(0, str.length() - seperator.length());
            }

            ArrayList temp = new ArrayList();
            int oldPos = 0;
            int newPos = str.indexOf(seperator);
            int parentLength = str.length();
            int subStrLength = seperator.length();
            if (newPos != -1) {
                newPos += subStrLength;
            }

            while (newPos <= parentLength && newPos != -1) {
                temp.add(str.substring(oldPos, newPos - subStrLength));
                oldPos = newPos;
                newPos = str.indexOf(seperator, newPos);
                if (newPos != -1) {
                    newPos += seperator.length();
                }
            }

            if (oldPos <= parentLength) {
                temp.add(str.substring(oldPos));
            }

            return (String[]) temp.toArray(new String[temp.size()]);
        } else {
            return tail ? new String[]{""} : new String[0];
        }
    }

    public static int count(String src, String key) {
        int count = 0;

        for (String temp = src; temp.indexOf(key) >= 0; temp = temp.substring(temp.indexOf(key) + key.length())) {
            ++count;
        }

        return count;
    }

    public static String replace(String src, String oldStr, String newStr) {
        StringBuffer result = new StringBuffer();
        boolean found = false;
        if (src != null && oldStr != null && newStr != null) {
            if (oldStr.length() == 0) {
                throw new IllegalArgumentException("illegal parameter");
            } else {
                int pos = 0;
                boolean var6 = false;

                while (pos < src.length()) {
                    int pos1 = src.indexOf(oldStr, pos);
                    if (pos1 < 0) {
                        pos1 = src.length();
                        found = false;
                    } else {
                        found = true;
                    }

                    result.append(src.substring(pos, pos1));
                    if (found) {
                        result.append(newStr);
                        pos = pos1 + oldStr.length();
                    } else {
                        pos = pos1;
                    }
                }

                return result.toString();
            }
        } else {
            throw new NullPointerException("parameter is null");
        }
    }

    public static String urlEncode(String url, String encode) {
        if (url == null) {
            return "";
        } else {
            byte[] dat;
            if (encode != null) {
                try {
                    dat = url.getBytes(encode);
                } catch (UnsupportedEncodingException var6) {
                    return "";
                }
            } else {
                dat = url.getBytes();
            }

            StringBuffer result = new StringBuffer();

            for (int i = 0; i < dat.length; ++i) {
                int ch = dat[i] & 255;
                if (isReserve(ch)) {
                    result.append("%");
                    result.append(Integer.toHexString(ch).toUpperCase());
                } else {
                    result.append((char) ch);
                }
            }

            return result.toString();
        }
    }

    private static boolean isReserve(int ch) {
        if (ch <= 126 && ch >= 32) {
            for (int i = 0; i < RESERVESNUM; ++i) {
                if (ch == reserve[i]) {
                    return true;
                }
            }

            return false;
        } else {
            return true;
        }
    }

    public static final String escapeHTMLTags(String in) {
        if (in == null) {
            return null;
        } else {
            int i = 0;
            int last = 0;
            char[] input = in.toCharArray();
            int len = input.length;

            StringBuffer out;
            for (out = new StringBuffer((int) ((double) len * 1.3D)); i < len; ++i) {
                char ch = input[i];
                if (ch <= '>') {
                    if (ch == '<') {
                        if (i > last) {
                            out.append(input, last, i - last);
                        }

                        last = i + 1;
                        out.append(LT_ENCODE);
                    } else if (ch == '>') {
                        if (i > last) {
                            out.append(input, last, i - last);
                        }

                        last = i + 1;
                        out.append(GT_ENCODE);
                    }
                }
            }

            if (last == 0) {
                return in;
            } else {
                if (i > last) {
                    out.append(input, last, i - last);
                }

                return out.toString();
            }
        }
    }

    public static final String escapeForXML(String in) {
        if (in == null) {
            return null;
        } else {
            int i = 0;
            int last = 0;
            char[] input = in.toCharArray();
            int len = input.length;

            StringBuffer out;
            for (out = new StringBuffer((int) ((double) len * 1.3D)); i < len; ++i) {
                char ch = input[i];
                if (ch <= '>') {
                    if (ch == '<') {
                        if (i > last) {
                            out.append(input, last, i - last);
                        }

                        last = i + 1;
                        out.append(LT_ENCODE);
                    } else if (ch == '&') {
                        if (i > last) {
                            out.append(input, last, i - last);
                        }

                        last = i + 1;
                        out.append(AMP_ENCODE);
                    } else if (ch == '"') {
                        if (i > last) {
                            out.append(input, last, i - last);
                        }

                        last = i + 1;
                        out.append(QUOTE_ENCODE);
                    }
                }
            }

            if (last == 0) {
                return in;
            } else {
                if (i > last) {
                    out.append(input, last, i - last);
                }

                return out.toString();
            }
        }
    }

    public static final String unescapeFromXML(String string) {
        string = replace(string, "&lt;", "<");
        string = replace(string, "&gt;", ">");
        string = replace(string, "&quot;", "\"");
        return replace(string, "&amp;", "&");
    }

    public static String replaceToken(String s) {
        int startToken = s.indexOf("${");
        int endToken = s.indexOf("}", startToken);
        String token = s.substring(startToken + 2, endToken);
        StringBuffer value = new StringBuffer();
        value.append(s.substring(0, startToken));
        value.append(System.getProperty(token));
        value.append(s.substring(endToken + 1));
        return value.toString();
    }

    public static String getFileSizeText(long size) {
        long num = size / 1024L + (long) (size % 1024L <= 0L ? 0 : 1);
        return num + "KB";
    }

    public static final synchronized String hash(String data) {
        if (digest == null) {
            try {
                digest = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException var2) {
                System.err.println("Failed to load the MD5 MessageDigest. System will be unable to function normally.");
                var2.printStackTrace();
            }
        }

        digest.update(data.getBytes());
        return encodeHex(digest.digest());
    }

    public static final String encodeHex(byte[] bytes) {
        StringBuffer buf = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; ++i) {
            if ((bytes[i] & 255) < 16) {
                buf.append("0");
            }

            buf.append(Long.toString((long) (bytes[i] & 255), 16));
        }

        return buf.toString();
    }

    public static final byte[] decodeHex(String hex) {
        char[] chars = hex.toCharArray();
        byte[] bytes = new byte[chars.length / 2];
        int byteCount = 0;

        for (int i = 0; i < chars.length; i += 2) {
            byte newByte = 0;
            newByte = (byte) (newByte | hexCharToByte(chars[i]));
            newByte = (byte) (newByte << 4);
            newByte |= hexCharToByte(chars[i + 1]);
            bytes[byteCount] = newByte;
            ++byteCount;
        }

        return bytes;
    }

    private static final byte hexCharToByte(char ch) {
        switch (ch) {
            case '0':
                return 0;
            case '1':
                return 1;
            case '2':
                return 2;
            case '3':
                return 3;
            case '4':
                return 4;
            case '5':
                return 5;
            case '6':
                return 6;
            case '7':
                return 7;
            case '8':
                return 8;
            case '9':
                return 9;
            case ':':
            case ';':
            case '<':
            case '=':
            case '>':
            case '?':
            case '@':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case '[':
            case '\\':
            case ']':
            case '^':
            case '_':
            case '`':
            default:
                return 0;
            case 'A':
                return 10;
            case 'B':
                return 11;
            case 'C':
                return 12;
            case 'D':
                return 13;
            case 'E':
                return 14;
            case 'F':
                return 15;
            case 'a':
                return 10;
            case 'b':
                return 11;
            case 'c':
                return 12;
            case 'd':
                return 13;
            case 'e':
                return 14;
            case 'f':
                return 15;
        }
    }

    public static String encodeBase64(String data) {
        return encodeBase64(trim(data).getBytes());
    }

    public static String encodeBase64(byte[] data) {
        int len = data.length;
        StringBuffer ret = new StringBuffer((len / 3 + 1) * 4);

        for (int i = 0; i < len; ++i) {
            int c = data[i] >> 2 & 63;
            ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            c = data[i] << 4 & 63;
            ++i;
            if (i < len) {
                c |= data[i] >> 4 & 15;
            }

            ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            if (i < len) {
                c = data[i] << 2 & 63;
                ++i;
                if (i < len) {
                    c |= data[i] >> 6 & 3;
                }

                ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            } else {
                ++i;
                ret.append('=');
            }

            if (i < len) {
                c = data[i] & 63;
                ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            } else {
                ret.append('=');
            }
        }

        return ret.toString();
    }

    public static String decodeBase64(String data) {
        return decodeBase64(data.getBytes());
    }

    public static String decodeBase64(byte[] data) {
        int len = data.length;
        StringBuffer ret = new StringBuffer(len * 3 / 4);

        for (int i = 0; i < len; ++i) {
            int c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(data[i]);
            ++i;
            int c1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(data[i]);
            c = c << 2 | c1 >> 4 & 3;
            ret.append((char) c);
            ++i;
            if (i < len) {
                c = data[i];
                if (61 == c) {
                    break;
                }

                c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf((char) c);
                c1 = c1 << 4 & 240 | c >> 2 & 15;
                ret.append((char) c1);
            }

            ++i;
            if (i < len) {
                c1 = data[i];
                if (61 == c1) {
                    break;
                }

                c1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf((char) c1);
                c = c << 6 & 192 | c1;
                ret.append((char) c);
            }
        }

        return ret.toString();
    }

    public static String encodeQuote(String data) {
        return data;
    }

    public static String decodeQuote(String data) {
        StringBuffer ret = new StringBuffer(data.length() / 3);
        char[] chars = data.toCharArray();

        for (int i = 0; i < chars.length; ++i) {
            if (chars[i] != '=') {
                ret.append(chars[i]);
            } else {
                ++i;

                StringBuffer hex;
                for (hex = new StringBuffer(); i < chars.length; ++i) {
                    hex.append(chars[i]);
                    ++i;
                    hex.append(chars[i]);
                    ++i;
                    if (i < chars.length && chars[i] != '=') {
                        break;
                    }
                }

                byte[] bytes = decodeHex(hex.toString());
                ret.append(new String(bytes));
            }
        }

        return ret.toString();
    }

    public static final String chopAtWord(String string, int length) {
        if (string == null) {
            return string;
        } else {
            char[] charArray = string.toCharArray();
            int sLength = string.length();
            if (length < sLength) {
                sLength = length;
            }

            int i;
            for (i = 0; i < sLength - 1; ++i) {
                if (charArray[i] == '\r' && charArray[i + 1] == '\n') {
                    return string.substring(0, i + 1);
                }

                if (charArray[i] == '\n') {
                    return string.substring(0, i);
                }
            }

            if (charArray[sLength - 1] == '\n') {
                return string.substring(0, sLength - 1);
            } else if (string.length() < length) {
                return string;
            } else {
                for (i = length - 1; i > 0; --i) {
                    if (charArray[i] == ' ') {
                        return string.substring(0, i).trim();
                    }
                }

                return string.substring(0, length);
            }
        }
    }

    public static String join(List list, String seperator, String warp) {
        return list != null && list.size() >= 1 ? join(list.toArray(), ",", "") : null;
    }

    public static String join(Object[] list, String seperator, String warp) {
        StringBuffer result = new StringBuffer();
        if (list == null) {
            return "";
        } else {
            if (warp == null) {
                warp = "";
            }

            for (int i = 0; i < list.length; ++i) {
                Object s = list[i];
                if (s != null) {
                    if (result.length() > 0) {
                        result.append(seperator);
                    }

                    result.append(warp).append(s.toString()).append(warp);
                }
            }

            return result.toString();
        }
    }

    public static String gbEncoding(String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";

        for (int byteIndex = 0; byteIndex < utfBytes.length; ++byteIndex) {
            String hexB = Integer.toHexString(utfBytes[byteIndex]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }

            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }

        return unicodeBytes;
    }

    public static boolean isASCIIString(String str) {
        if (str != null && str.length() != 0) {
            byte[] bytes = str.getBytes();

            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static String duplicate(String source, int count) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < count; ++i) {
            sb.append(source);
        }

        return sb.toString();
    }

    public static String fillCharAtHeadForNumber(int source, String count, char fillchr) {
        int i = getAsInt(count);
        String str = String.valueOf(source);
        if (str.length() < i) {
            for (int j = str.length(); j < i; ++j) {
                str = fillchr + str;
            }
        }

        return str;
    }

    public static String fillCharAtHead(String source, String count, char fillchr) {
        int i = getAsInt(count);
        if (source.length() < i) {
            for (int j = source.length(); j < i; ++j) {
                source = fillchr + source;
            }
        }

        return source;
    }

    public static String fillCharAtTail(String source, String count, char fillchr) {
        int i = getAsInt(count);
        if (source.length() < i) {
            for (int j = source.length(); j < i; ++j) {
                source = source + fillchr;
            }
        }

        return source;
    }

    public static String pad(String words, String filling, int length) {
        for (int i = words.length(); i < length; ++i) {
            words = filling + words;
        }

        return words;
    }

    public static String rpad(String words, String filling, int length) {
        for (int i = words.length(); i < length; ++i) {
            words = words + filling;
        }

        return words;
    }

    public static boolean in(String str, String[] array) {
        for (int i = 0; i < array.length; ++i) {
            if (str == null && array[i] == null) {
                return true;
            }

            if (str != null && str.equals(array[i])) {
                return true;
            }
        }

        return false;
    }

    public static boolean empty(String str) {
        return str == null || "".equals(str);
    }

    public static boolean notEmpty(String str) {
        return !empty(str);
    }

    public static String getContentType(String type) {
        if (type == null) {
            type = "";
        }

        type = type.toLowerCase();
        String t = "";
        if ("bmp".equals(type)) {
            t = "image/bmp";
        } else if ("gif".equals(type)) {
            t = "image/gif";
        } else if (!"jpg".equals(type) && !"jpeg".equals(type)) {
            if ("tiff".equals(type)) {
                t = "image/tiff";
            } else if ("dcx".equals(type)) {
                t = "image/x-dcx";
            } else if ("pcx".equals(type)) {
                t = "image/x-pcx";
            } else if (!"html".equals(type) && !"htm".equals(type)) {
                if ("txt".equals(type)) {
                    t = "text/plain";
                } else if ("xml".equals(type)) {
                    t = "text/xml";
                } else if ("afp".equals(type)) {
                    t = "application/afp";
                } else if ("pdf".equals(type)) {
                    t = "application/pdf";
                } else if ("rtf".equals(type)) {
                    t = "application/rtf";
                } else if ("doc".equals(type)) {
                    t = "application/msword";
                } else if ("xls".equals(type)) {
                    t = "application/vnd.ms-excel";
                } else if ("ppt".equals(type)) {
                    t = "application/vnd.ms-powerpoint";
                } else if ("vsd".equals(type)) {
                    t = "application/vnd.visio";
                }
            } else {
                t = "text/html";
            }
        } else {
            t = "image/jpeg";
        }

        return t;
    }

    public static int indexof(String[] strs, String str) {
        if (str != null && strs != null) {
            for (int i = 0; i < strs.length; ++i) {
                if (str.equals(strs[i])) {
                    return i;
                }
            }

            return -1;
        } else {
            return -1;
        }
    }

    public static boolean checkIntercross(String[] strs1, String[] strs2) {
        if (strs1 != null && strs2 != null) {
            for (int i = 0; i < strs1.length; ++i) {
                for (int k = 0; k < strs2.length; ++k) {
                    if (strs1[i].equals(strs2[k])) {
                        return true;
                    }
                }
            }

            return false;
        } else {
            return false;
        }
    }

    public static String toUpperCaseFirst(String str) {
        if (isEmpty(str)) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder(str);
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            str = sb.toString();
            return str;
        }
    }

    public static String toUpperCase(String s) {
        return s == null ? null : s.toUpperCase();
    }

    public static String toLowerCase(String s) {
        return s == null ? null : s.toLowerCase();
    }

    public static String substring(String s, int beginIndex, int endIndex) {
        return s == null ? null : s.substring(beginIndex, endIndex);
    }

    public static String bytes2HexString(byte[] b) {
        String ret = "";

        for (int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            ret = ret + hex.toUpperCase();
        }

        return ret;
    }

    public static String encodeHex(String msg) {
        byte[] bytes = null;

        try {
            bytes = msg.getBytes("UTF-8");
        } catch (UnsupportedEncodingException var8) {
            var8.printStackTrace();
        }

        StringBuffer buff = new StringBuffer(bytes.length * 4);
        int n = 0;

        for (int i = 0; i < bytes.length; ++i) {
            String b = Integer.toHexString(bytes[i]);
            if (bytes[i] > 0) {
                buff.append("00");
                buff.append(b);
                ++n;
            } else {
                char a = msg.charAt((i - n) / 2 + n);
                b = Integer.toHexString(a);
                buff.append(b.substring(0, 4));
                ++i;
            }
        }

        return buff.toString();
    }

    public static String encodeHexToBit(String hexStr) {
        String bitStr = "";
        if (hexStr.length() % 2 != 0) {
            hexStr = "0" + hexStr;
        }

        for (int i = 0; i < hexStr.length() / 2; ++i) {
            byte b = 0;
            int val = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
            b = (byte) val;
            bitStr = bitStr + (byte) (b >> 7 & 1) + (byte) (b >> 6 & 1) + (byte) (b >> 5 & 1) + (byte) (b >> 4 & 1) + (byte) (b >> 3 & 1) + (byte) (b >> 2 & 1) + (byte) (b >> 1 & 1) + (byte) (b >> 0 & 1);
        }

        return bitStr;
    }

    public static String htmlEncode(String strInput) {
        if (strInput != null && strInput.length() != 0) {
            StringBuffer builder = new StringBuffer(strInput.length() * 2);
            CharacterIterator it = new StringCharacterIterator(strInput);

            for (char ch = it.first(); ch != '\uffff'; ch = it.next()) {
                if ((ch <= '`' || ch >= '{') && (ch <= '@' || ch >= '[') && ch != ' ' && (ch <= '/' || ch >= ':') && ch != '.' && ch != ',' && ch != '-' && ch != '_') {
                    builder.append("&#" + ch + ";");
                } else {
                    builder.append(ch);
                }
            }

            return builder.toString();
        } else {
            return "";
        }
    }

    public static String clearHtmlTag(String htmlStr) {
        if (htmlStr == null) {
            return "";
        } else {
            String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";
            String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>";
            String regEx_html = "<[^>]+>";
            String regEx_space = "\\s*|\t|\r|\n";
            Pattern p_script = Pattern.compile(regEx_script, 2);
            Matcher m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll("");
            Pattern p_style = Pattern.compile(regEx_style, 2);
            Matcher m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll("");
            Pattern p_html = Pattern.compile(regEx_html, 2);
            Matcher m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll("");
            Pattern p_space = Pattern.compile(regEx_space, 2);
            Matcher m_space = p_space.matcher(htmlStr);
            htmlStr = m_space.replaceAll("");
            htmlStr = htmlStr.trim();
            htmlStr = htmlStr.replaceAll("&nbsp;", "");
            return htmlStr;
        }
    }

    public static String removeNonVisableChar(String str) {
        if (str == null) {
            return str;
        } else {
            StringBuffer buf = new StringBuffer(str);

            for (int ii = buf.length() - 1; ii >= 0; --ii) {
                byte[] bytes = String.valueOf(buf.charAt(ii)).getBytes();
                if (bytes != null && bytes.length <= 2 && bytes.length > 0) {
                    int var10000 = bytes.length;
                    if (bytes.length != 2 && (buf.charAt(ii) < ' ' || buf.charAt(ii) > '~')) {
                        buf.setLength(buf.length() - 1);
                    }
                }
            }

            return buf.toString();
        }
    }


    public static boolean isMessyCode(String strName) {
        Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
        Matcher m = p.matcher(strName);
        String after = m.replaceAll("");
        String temp = after.replaceAll("\\p{P}", "");
        char[] ch = temp.trim().toCharArray();
        float chLength = (float) ch.length;
        float count = 0.0F;

        for (int i = 0; i < ch.length; ++i) {
            char c = ch[i];
            if (!Character.isLetterOrDigit(c) && !isChinese(c)) {
                ++count;
            }
        }

        float result = count / chLength;
        if ((double) result > 0.4D) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
    }

    /**
     * 将一个int数字转换为二进制的字符串形式。
     *
     * @param num    需要转换的int类型数据
     * @param digits 要转换的二进制位数，位数不足则在前面补0
     * @return 二进制的字符串形式
     */
    public static String toBinary(int num, int digits) {
        int value = 1 << digits | num;
        String bs = Integer.toBinaryString(value); //0x20 | 这个是为了保证这个string长度是6位数
        return new String(bs.substring(1));
    }

    public static void main(String[] args) throws Exception {

        System.out.println(toHexString("#"));
       /* String s = "id,server_id,server_ip,logtime,user,imei,dev_type,client_ip,op_module,op_type,op_result,in_time";
        System.err.println(toUpperCase(s));*/
    }
}
